package com.shen.provider.controller;

import com.shen.apicommons.entity.Student;
import com.shen.apicommons.util.CommentUtil;
import com.shen.provider.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

@RestController
@Slf4j
@RequestMapping("/sc")
public class StudentController {

    @Autowired
    private StudentService studentService;
    @Value("${server.port}")
    private String serverPort;
    @PostMapping("/addStudent")
    public CommentUtil addStudent(@RequestBody Student student){
        int result = studentService.addStudent(student);
        log.info("****插入结果,{}",result);
        if (result > 0){
           return new CommentUtil(200,"插入数据成功,serverPort:"+serverPort,result);
        }else {
           return new CommentUtil(444,"插入数据失败");
        }
    }

    @GetMapping("/selectStudent/get/{id}")
    public CommentUtil selectStudent(@PathVariable("id") Integer id){
        Student student = studentService.queryStudent(id);
        log.info("****查询结果"+student);
        if (Optional.ofNullable(student).isPresent()){
            return new CommentUtil(200,"查询成功,serverPort:"+serverPort,student);
        }else {
            return new CommentUtil(444,"查询失败");
        }
    }
}
